package office.write.jacob.word.core;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class test {
   private static    Dispatch document;
   private static   Dispatch documents ;
   private static    Dispatch selection ;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//启用word 生成ActiveXComponent对象
		ActiveXComponent app = new ActiveXComponent("Word.Application");
		//System.out.println(System.getProperty("user.dir"));
		String templateWordPath = System.getProperty("user.dir")+"\\upload\\user.doc";
		String newWordPath = System.getProperty("user.dir")+"\\upload\\user1.doc";
		//File templateWordFile  = new File(System.getProperty("user.dir")+"\\upload\\user.doc");
       // File newWordFile = new File(System.getProperty("user.dir")+"\\upload\\user1.doc");
	    //boolean flag = false;
	    //Dispatch  xlo = app.getObject();
		   boolean visible= false;  
		   //设置word不可见  
		      app.setProperty("Visible", new Variant(visible)); 
		       documents = Dispatch.get(app, "Documents").toDispatch(); 
		     // Dispatch docs = app.getProperty("Documents").toDispatch();  
		     //  document = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{templateWordPath, new Variant(false), new Variant(true)}, new int[1]).toDispatch(); //打开word文件  
		       document = Dispatch.call(documents, "Open", templateWordPath).toDispatch(); 
		           selection =app.getProperty("Selection").toDispatch();  
		     
		      //得到一个组件  
		     // System.out.println("Selection");  
		     // Dispatch find = app.call(selection, "Find").toDispatch();  
		        /**
		         * 表格索引从1开始
		         */
	             String tableRows = test.getTableRowsCount("1");
	             System.out.println(tableRows);
	             String tableColumn = test.getTableColumnCount("1");
	             System.out.println(tableColumn);
	             test.addRow(1);
	             test.putTxtToCell(1, 2, 1, "张三");
	             //保存文件  
	             Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] {newWordPath, new Variant(0)}, new int[1]);  
	             //作为word格式保存到目标文件  
	             Variant f = new Variant(false);  
	             Dispatch.call(document, "Close", f);  
	             
	             
	}
	/** 
	* 获取word文件中某个表格的行数 
	* @param tableIndex 
	* @param String 
	*/ 
	public static String getTableRowsCount(String tableIndex) { 
	if(tableIndex == null || "".equals(tableIndex)) 
	tableIndex = "0"; 
	// 所有表格 
	  Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); 
	  // 要填充的表格 
	  Dispatch table = Dispatch.call(tables, "item", new Variant(tableIndex)) 
	    .toDispatch(); 
	  // 表格的所有行 
	  Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); 
	  
	   return new String(""+Dispatch.get(rows, "Count").getInt()); 
	} 
	/** 
	* 获取word文件中某个表格的行数 
	* @param tableIndex 
	* @param String 
	*/ 
	public static String getTableColumnCount(String tableIndex) { 
		if(tableIndex == null || "".equals(tableIndex)) 
			tableIndex = "0"; 
			// 所有表格 
			  Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); 
			  // 要填充的表格 
			  Dispatch table = Dispatch.call(tables, "item", new Variant(tableIndex)) 
			    .toDispatch(); 
			  // 表格的所有列
			  Dispatch rows = Dispatch.get(table, "Columns").toDispatch(); 
			  
			  return new String(""+Dispatch.get(rows, "Count").getInt()); 
			} 
	
	//增加一行
	public static void addRow(int tableIndex) {
	Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
	// 要填充的表格
	Dispatch table = Dispatch.call(tables, "item", new Variant(tableIndex))
	  .toDispatch();
	// 表格的所有行
	Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
	Dispatch.call(rows, "Add");
	}
    /** *//** 
     * 在指定的单元格里填写数据 
     *     
     * @param tableIndex 
     * @param cellRowIdx 
     * @param cellColIdx 
     * @param txt 
     */ 
    public static void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx, 
                    String txt) { 
            // 所有表格 
            Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); 
            // 要填充的表格 
            Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)) 
                            .toDispatch(); 
            Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), 
                            new Variant(cellColIdx)).toDispatch(); 
            Dispatch.call(cell, "Select"); 
            Dispatch.put(selection, "Text", txt); 
    } 
}

